<?php

namespace app\user\tools\tencent;

use app\admin\logic\system\Config as SystemConfig;
use app\common\exception\LogicException;
use GuzzleHttp\Client;
use think\helper\Str;

/**
 * 腾讯H5内置浏览器分享类
 * Author 亓官雨树<lucky.max@foxmail.com>
 * Date 2023-09-05
 */
class Share
{

    protected $appId;
    protected $secret;
    //
    protected $accessTokenUri = "https://api.weixin.qq.com/cgi-bin/token";
    protected $accessToken = "";
    //
    protected $jsapiTicketUri = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket';
    protected $jsapiTicket = '';
    //

    /**
     * 构造函数
     */
    public function __construct()
    {
        $official     = SystemConfig::get('system.config.official', ['appid' => '', 'secret' => ''], request()->mhm_id);
        $this->appId  = $official['appid'];
        $this->secret = $official['secret'];
    }

    /**
     * getAccessToken
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 23/09/05
     */
    public function getAccessToken()
    {
        $token = cache('wxShareAccessToken');
        if ($token) {
            $this->accessToken = $token;
        } else {
            $param    = [
                'grant_type' => 'client_credential',
                'appid'      => $this->appId,
                'secret'     => $this->secret,
            ];
            $param    = http_build_query($param);
            $client   = new Client();
            $res      = $client->get($this->accessTokenUri . '?' . $param);
            $response = $res->getBody()->getContents();

            $result = json_decode($response);
            if ($result->access_token) {
                $this->accessToken = $result->access_token;
                cache('wxShareAccessToken', $result->access_token, $result->expires_in);
            } else {
                throw new LogicException($result->errmsg);
            }
        }
    }

    /**
     * getJsapiTicket
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 23/09/05
     */
    public function getJsapiTicket()
    {
        $ticket = cache('wxShareJsapiTicket');
        if ($ticket) {
            $this->jsapiTicket = $ticket;
        } else {
            $this->getAccessToken();
            $param    = [
                'access_token' => $this->accessToken,
                'type'         => 'jsapi'
            ];
            $param    = http_build_query($param);
            $client   = new Client();
            $res      = $client->get($this->jsapiTicketUri . '?' . $param);
            $response = $res->getBody()->getContents();

            $result = json_decode($response);
            if (0 === $result->errcode) {
                $this->jsapiTicket = $result->ticket;
                cache('wxShareJsapiTicket', $result->ticket, $result->expires_in);
            } else {
                throw new LogicException($result->errmsg);
            }
        }
    }

    /**
     * getSignature
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 23/09/05
     *
     * @param $url
     * @return string
     */
    public function getSignature($url)
    {
        $this->getJsapiTicket();
        $signData = [
            'noncestr'     => Str::random(16),
            'jsapi_ticket' => $this->jsapiTicket,
            'timestamp'    => time(),
            // 'url'          => $url
        ];
        ksort($signData);
        $sign                  = http_build_query($signData);
        $signData['signature'] = sha1($sign . '&url=' . $url);
        $signData['appId']     = $this->appId;
        unset($signData['jsapi_ticket']);
        return $signData;
    }

    public function getSignData($url)
    {

    }

}
